home *** CD-ROM | disk | FTP | other *** search
/ Aminet 28 / Aminet 28 (1998)(GTI - Schatztruhe)[!][Dec 1998].iso / Aminet / dev / misc / Compressor.lha / CompressorClass / Examples / ANSI-C / Class_Test.c next >
Encoding:
C/C++ Source or Header  |  1998-09-23  |  32.1 KB  |  1,068 lines

  1. /* -- ----------------------------------------------------------------- -- *
  2.  * -- Program.....: Class_Test.c                                        -- *
  3.  * -- Author......: Daniel Kasmeroglu <raptor@cs.tu-berlin.de>          -- *
  4.  * -- Description.: Demonstration of the Compressor.class               -- *
  5.  * -- ----------------------------------------------------------------- -- *
  6.  * -- History                                                           -- *
  7.  * --                                                                   -- *
  8.  * --   0.1 (31. August    1998) - Started with writing.                -- *
  9.  * --   1.0 (17. September 1998) - Finished writing of E-Version.       -- *
  10.  * --   1.0 (17. September 1998) - Finished C version.                  -- *
  11.  * --                                                                   -- *
  12.  * -- ----------------------------------------------------------------- -- */
  13.  
  14. /* -- ----------------------------------------------------------------- -- *
  15.  * --                              Includes                             -- *
  16.  * -- ----------------------------------------------------------------- -- */
  17.  
  18. #include <stdio.h>
  19. #include <string.h>
  20.  
  21. #include <libraries/compressor.h>
  22. #include <libraries/iffparse.h>
  23. #include <libraries/xpk.h>
  24.  
  25. #include <utility/tagitem.h>
  26. #include <utility/hooks.h>
  27.  
  28. #include <dos/dosextens.h>
  29. #include <dos/rdargs.h>
  30. #include <dos/dos.h>
  31.  
  32. #include <intuition/classusr.h>
  33. #include <intuition/classes.h>
  34.  
  35. #include <prefs/prefhdr.h>
  36.  
  37. #include <exec/memory.h>
  38.  
  39. #include <pragma/compressor_lib.h>
  40. #include <pragma/intuition_lib.h>
  41. #include <pragma/iffparse_lib.h>
  42. #include <pragma/exec_lib.h>
  43.  
  44. #include <clib/compressor_protos.h>
  45. #include <clib/iffparse_protos.h>
  46. #include <clib/alib_protos.h>
  47. #include <clib/dos_protos.h>
  48.  
  49.  
  50. /* -- ----------------------------------------------------------------- -- *
  51.  * --                            Structures                             -- *
  52.  * -- ----------------------------------------------------------------- -- */
  53.  
  54. struct Msg {
  55.   ULONG MethodID;
  56. };
  57.  
  58.  
  59. /* -- ----------------------------------------------------------------- -- *
  60.  * --                             Constants                             -- *
  61.  * -- ----------------------------------------------------------------- -- */
  62.  
  63. #define help_ClearCON  printf( "%c[0;0H%c[J", 27, 27 );
  64.  
  65.  
  66. /* -- ----------------------------------------------------------------- -- *
  67.  * --                           Hook-Routines                           -- *
  68.  * -- ----------------------------------------------------------------- -- */
  69.  
  70. /// hoo_CLIProgress
  71. LONG __saveds hoo_CLIProgress( register __a1 struct XpkProgress *cli_msg ) {
  72.  
  73.   if (cli_msg->xp_Type == XPKPROG_START)
  74.     {
  75.       help_ClearCON
  76.       printf( "\n    File....: '%s'\n"    , cli_msg->xp_FileName );
  77.       printf( "    Size....: %lu Bytes\n" , cli_msg->xp_ULen     );
  78.       printf( "\r    Done....: %-3u %%"   , cli_msg->xp_Done     );
  79.       fflush( stdout );
  80.     }
  81.   else {
  82.     printf( "\r    Done....: %-3u %%" , cli_msg->xp_Done );
  83.     fflush( stdout );
  84.   };
  85.  
  86.   if (cli_msg->xp_Type == XPKPROG_END) printf( "\n    End of (de)compression !\n" );
  87.  
  88.   return SetSignal(0L,SIGBREAKF_CTRL_C)&SIGBREAKF_CTRL_C;
  89.  
  90. };
  91. ///
  92.  
  93.  
  94. /* -- ----------------------------------------------------------------- -- *
  95.  * --                           Declarations                            -- *
  96.  * -- ----------------------------------------------------------------- -- */
  97.  
  98. struct Hook glo_clihook  = {{0L},(HOOKFUNC)hoo_CLIProgress};
  99. APTR        glo_mempool  = NULL;
  100. APTR        glo_filemem  = 0;
  101. ULONG       glo_filesize = NULL;
  102.  
  103. struct Library *iffparsebase;
  104. struct Library *compressorbase;
  105. struct Library *intuitionbase;
  106. struct Library *dosbase;
  107. struct Library *gfxbase;
  108.  
  109.  
  110. /* -- ----------------------------------------------------------------- -- *
  111.  * --                         Helping Routines                          -- *
  112.  * -- ----------------------------------------------------------------- -- */
  113.  
  114. /// help_WaitReturn
  115. void help_WaitReturn() {
  116. struct RDArgs   *wai_rdargs;
  117. ULONG           wai_var;
  118.   printf( "\n\n< PRESS RETURN TO CONTINUE >\n" );
  119.   wai_rdargs = ReadArgs( "ARG", (LONG *)&wai_var, NULL );
  120.   if (wai_rdargs != NULL) FreeArgs( wai_rdargs );
  121. };
  122. ///
  123.  
  124. /// help_ShowXPKError
  125. // Simply prints out the name of the passed XPK-Error.
  126. // It's much more helpful then showing a negative value.
  127. void help_ShowXPKError( LONG sho_value ) {
  128.  
  129.   switch(sho_value) {
  130.   case XPKERR_NOFUNC       : printf( "XPKERR_NOFUNC\n"      ) ; break ;
  131.   case XPKERR_NOFILES      : printf( "XPKERR_NOFILES\n"     ) ; break ;
  132.   case XPKERR_IOERRIN      : printf( "XPKERR_IOERRIN\n"     ) ; break ;
  133.   case XPKERR_IOERROUT     : printf( "XPKERR_IOERROUT\n"    ) ; break ;
  134.   case XPKERR_CHECKSUM     : printf( "XPKERR_CHECKSUM\n"    ) ; break ;
  135.   case XPKERR_VERSION      : printf( "XPKERR_VERSION\n"     ) ; break ;
  136.   case XPKERR_NOMEM        : printf( "XPKERR_NOMEM\n"       ) ; break ;
  137.   case XPKERR_LIBINUSE     : printf( "XPKERR_LIBINUSE\n"    ) ; break ;
  138.   case XPKERR_WRONGFORM    : printf( "XPKERR_WRONGFORM\n"   ) ; break ;
  139.   case XPKERR_SMALLBUF     : printf( "XPKERR_SMALLBUF\n"    ) ; break ;
  140.   case XPKERR_LARGEBUF     : printf( "XPKERR_LARGEBUF\n"    ) ; break ;
  141.   case XPKERR_WRONGMODE    : printf( "XPKERR_WRONGMODE\n"   ) ; break ;
  142.   case XPKERR_NEEDPASSWD   : printf( "XPKERR_NEEDPASSWD\n"  ) ; break ;
  143.   case XPKERR_CORRUPTPKD   : printf( "XPKERR_CORRUPTPKD\n"  ) ; break ;
  144.   case XPKERR_MISSINGLIB   : printf( "XPKERR_MISSINGLIB\n"  ) ; break ;
  145.   case XPKERR_BADPARAMS    : printf( "XPKERR_BADPARAMS\n"   ) ; break ;
  146.   case XPKERR_EXPANSION    : printf( "XPKERR_EXPANSION\n"   ) ; break ;
  147.   case XPKERR_NOMETHOD     : printf( "XPKERR_NOMETHOD\n"    ) ; break ;
  148.   case XPKERR_ABORTED      : printf( "XPKERR_ABORTED\n"     ) ; break ;
  149.   case XPKERR_TRUNCATED    : printf( "XPKERR_TRUNCATED\n"   ) ; break ;
  150.   case XPKERR_WRONGCPU     : printf( "XPKERR_WRONGCPU\n"    ) ; break ;
  151.   case XPKERR_PACKED       : printf( "XPKERR_PACKED\n"      ) ; break ;
  152.   case XPKERR_NOTPACKED    : printf( "XPKERR_NOTPACKED\n"   ) ; break ;
  153.   case XPKERR_FILEEXISTS   : printf( "XPKERR_FILEEXISTS\n"  ) ; break ;
  154.   case XPKERR_OLDMASTLIB   : printf( "XPKERR_OLDMASTLIB\n"  ) ; break ;
  155.   case XPKERR_OLDSUBLIB    : printf( "XPKERR_OLDSUBLIB\n"   ) ; break ;
  156.   case XPKERR_NOCRYPT      : printf( "XPKERR_NOCRYPT\n"     ) ; break ;
  157.   case XPKERR_NOINFO       : printf( "XPKERR_NOINFO\n"      ) ; break ;
  158.   case XPKERR_LOSSY        : printf( "XPKERR_LOSSY\n"       ) ; break ;
  159.   case XPKERR_NOHARDWARE   : printf( "XPKERR_NOHARDWARE\n"  ) ; break ;
  160.   case XPKERR_BADHARDWARE  : printf( "XPKERR_BADHARDWARE\n" ) ; break ;
  161.   case XPKERR_WRONGPW      : printf( "XPKERR_WRONGPW\n"     ) ; break ;
  162.   };
  163.  
  164. };
  165. ///
  166.  
  167. /// help_ReadValue
  168. LONG help_ReadValue() {
  169. struct RDArgs    *rea_rdargs;
  170. ULONG            rea_value;
  171.   rea_rdargs = ReadArgs( "VALUE/N", (LONG *)&rea_value, NULL );
  172.   rea_value  = rea_value ? *(ULONG *)rea_value : 0;
  173.   if (rea_rdargs != NULL) FreeArgs( rea_rdargs );
  174.   return (LONG)rea_value;
  175. };
  176. ///
  177.  
  178. /// help_SetAttrsA
  179. void help_SetAttrsA( APTR set_object, ULONG set_attrid, ULONG set_data ) {
  180. struct TagItem   set_tags[2];
  181.   set_tags[0].ti_Tag   = set_attrid;
  182.   set_tags[0].ti_Data  = set_data;
  183.   set_tags[1].ti_Tag   = TAG_END;
  184.   SetAttrsA( set_object, &set_tags[0] );
  185. };
  186. ///
  187.  
  188. /// help_ReadString
  189. void help_ReadString( STRPTR rea_buffer ) {
  190. struct RDArgs   *rea_rdargs;
  191. ULONG           rea_adr;
  192.   rea_adr    = NULL;
  193.   rea_rdargs = ReadArgs( "STRING", (LONG *)&rea_adr, NULL );
  194.   if (rea_rdargs != NULL) {
  195.     if (rea_adr != NULL)
  196.       strcpy( rea_buffer, (STRPTR)rea_adr );
  197.     else
  198.       strcpy( rea_buffer, "" );
  199.     FreeArgs( rea_rdargs );
  200.   };
  201.   else {
  202.     strcpy( rea_buffer, "" );
  203.   };
  204. };
  205. ///
  206.  
  207. /// help_FileLength
  208. ULONG help_FileLength( STRPTR fil_path ) {
  209. struct FileInfoBlock   fil_fib;
  210. ULONG                  fil_length;
  211. BPTR                   fil_lock;
  212.  
  213.   fil_length = -1;
  214.   fil_lock   = Lock( fil_path, SHARED_LOCK );
  215.   if (fil_lock != NULL) {
  216.     if (Examine( fil_lock, &fil_fib ) != NULL)
  217.       fil_length =  fil_fib.fib_Size;
  218.     UnLock( fil_lock );
  219.   };
  220.  
  221.   return fil_length;
  222.  
  223. };
  224. ///
  225.  
  226. /// help_WriteDummyFile
  227. // This little routines writes files of a given size.
  228. // The files are filled with shit but they may be
  229. // used for demonstrations.
  230. ULONG help_WriteDummyFile( STRPTR wri_file, ULONG wri_size ) {
  231. BPTR wri_han;
  232. APTR wri_mem;
  233.  
  234.   // allocate memory with the requested size
  235.   wri_mem = AllocMem( wri_size, 0 );
  236.   if (wri_mem != NULL) {
  237.  
  238.     // open the file
  239.     wri_han = Open( wri_file, MODE_NEWFILE );
  240.     if (wri_han != NULL) {
  241.       // write the memory area into this file
  242.       Write( wri_han, wri_mem, wri_size );
  243.       Close( wri_han );
  244.     };
  245.     FreeMem( wri_mem, wri_size );
  246.  
  247.   };
  248.  
  249.   // was writing successful
  250.   if (help_FileLength( wri_file ) == wri_size)
  251.     return TRUE;
  252.   else
  253.     DeleteFile( wri_file );
  254.  
  255.   return FALSE;
  256. };
  257. ///
  258.  
  259. /// help_LoadFile
  260. void help_LoadFile( STRPTR loa_path ) {
  261. BPTR loa_han;
  262.  
  263.   if (glo_filemem != NULL) FreeMem( glo_filemem, glo_filesize );
  264.  
  265.   glo_filesize = help_FileLength( loa_path );
  266.   glo_filemem  = AllocMem( glo_filesize, MEMF_PUBLIC );
  267.   if (glo_filemem != NULL) {
  268.  
  269.     loa_han = Open( loa_path, MODE_OLDFILE );
  270.     if (loa_han != NULL) {
  271.  
  272.       if (Read( loa_han, glo_filemem, glo_filesize ) != glo_filesize) {
  273.         FreeMem( glo_filemem, glo_filesize );
  274.         glo_filemem  = NULL;
  275.         glo_filesize = 0;
  276.       };
  277.  
  278.       Close( loa_han );
  279.     };
  280.     else {
  281.       FreeMem( glo_filemem, glo_filesize );
  282.       glo_filemem  = NULL;
  283.       glo_filesize = 0;
  284.     };
  285.   };
  286. };
  287. ///
  288.  
  289.  
  290. /* -- ----------------------------------------------------------------- -- *
  291.  * --                          Demonstrations                           -- *
  292.  * -- ----------------------------------------------------------------- -- */
  293.  
  294. /// dem_ShowConfiguration
  295. void dem_ShowConfiguration( APTR dem_object ) {
  296. struct XpkPackerInfo  *dem_packerinfo;
  297. struct XpkMode        *dem_packermode;
  298. ULONG                 dem_method;
  299. ULONG                 dem_mode;
  300.  
  301.   // This procedure simply gets some information from the given
  302.   // object and prints them out. It's very simpel as you can see.
  303.   GetAttr( CCA_METHOD, dem_object, &dem_method );
  304.   printf( "\n\nMethod..............: '%s'\n", dem_method );
  305.  
  306.   GetAttr( CCA_XPKPACKERINFO , dem_object, (ULONG *)&dem_packerinfo );
  307.   printf( "LongName............: '%s'\n", dem_packerinfo->xpi_LongName );
  308.   printf( "Description.........: '%s'\n", dem_packerinfo->xpi_Description );
  309.  
  310.   GetAttr( CCA_MODE, dem_object, &dem_mode );
  311.   printf( "Mode................: %d\n", dem_mode );
  312.  
  313.   GetAttr( CCA_XPKMODE, dem_object, (ULONG *)&dem_packermode );
  314.   printf( "Mode-Description....: '%s'\n", dem_packermode->xm_Description );
  315.   printf( "Encryption..........: '%s'\n", (dem_packerinfo->xpi_Flags & XPKIF_ENCRYPTION) ? "possible" : "not possible" );
  316.  
  317.   GetAttr( CCA_PASSWORD, dem_object, (ULONG *)&dem_method );
  318.   printf( "Password............: '%s'\n", dem_method );
  319.  
  320.   GetAttr( CCA_MEMPOOL, dem_object, (ULONG *)&dem_method );
  321.   printf( "Memory pool.........: %s\n", dem_method ? "installed" : "not installed" );
  322.  
  323.   GetAttr( CCA_PROGRESSHOOK, dem_object, (ULONG *)&dem_method );
  324.   printf( "Progress-Hook.......: %s\n", dem_method ? "installed" : "not installed" );
  325.  
  326.   GetAttr( CCA_HIDEPASSWORD, dem_object, (ULONG *)&dem_method );
  327.   printf( "Flags...............: %s", dem_method ? "CCF_HIDEPASSWORD\n                      " : "" );
  328.  
  329.   GetAttr( CCA_INTERNALPROGRESS, dem_object, (ULONG *)&dem_method );
  330.   printf( "%s", dem_method ? "CCF_INTERNALPROGRESS\n                      " : "" );
  331.  
  332.   GetAttr( CCA_SCREENLOCKED, dem_object, (ULONG *)&dem_method );
  333.   printf( "%s", dem_method ? "CCF_SCREENLOCKED\n" : "" );
  334.  
  335.   help_WaitReturn();
  336.  
  337. };
  338. ///
  339.  
  340. /// dem_SelectMethod
  341. void dem_SelectMethod( APTR dem_object ) {
  342. ULONG *dem_list;
  343. ULONG dem_count;
  344. ULONG dem_end;
  345. ULONG dem_run;
  346. LONG  dem_choice;
  347.  
  348.   GetAttr( CCA_METHODLIST, dem_object, (ULONG *)&dem_list );
  349.   GetAttr( CCA_NUMPACKERS, dem_object, &dem_count );
  350.  
  351.   dem_end = 0;
  352.  
  353.   for(;;) {
  354.  
  355.     help_ClearCON
  356.  
  357.     printf( "\nSelect a method by entering the preceding number !\n\n" );
  358.  
  359.     // print out the list of available packer
  360.     printf( "\n" );
  361.     for ( dem_run = 1; dem_run<=dem_count; dem_run++ ) {
  362.       printf( " %3d. %s   ", dem_run, dem_list[ dem_run - 1 ] );
  363.       if (dem_run % 5 == 0) printf( "\n" );
  364.     };
  365.  
  366.     printf( "\n\nYour choice => " );
  367.     fflush( stdout );
  368.     dem_choice = help_ReadValue();
  369.  
  370.     if ((dem_choice > 0) && (dem_choice <= dem_count)) dem_end = 1;
  371.  
  372.     if (dem_end != 0) break;
  373.  
  374.   };
  375.  
  376.   dem_choice = dem_choice - 1;
  377.  
  378.   // both attributes are having the same effect, so you can use
  379.   // both calls. naturally you only need one of the following lines
  380.   // and you should prefer the attribute "CCA_METHODINDEX" because
  381.   // setting this is much faster than "CCA_METHOD". the reason is
  382.   // simple because my object have to search the method in the list
  383.   // and this results in some string-comparisons. if you are passing
  384.   // the name of a method which isn't available (or other shit) this
  385.   // will be ignored. all will be left unchanged.
  386.   help_SetAttrsA( dem_object, CCA_METHODINDEX, (ULONG)dem_choice );
  387.   help_SetAttrsA( dem_object, CCA_METHOD, (ULONG)dem_list[ dem_choice ] );
  388.  
  389. };
  390. ///
  391.  
  392. /// dem_SelectMode
  393. void dem_SelectMode( APTR dem_object ) {
  394. LONG dem_mode;
  395.  
  396.   printf( "\nEnter a value (1..100) => " );
  397.   fflush( stdout );
  398.   dem_mode = help_ReadValue();
  399.  
  400.   // setting a value lower than 1 or higher than 100 will
  401.   // leave my object unchanged.
  402.   help_SetAttrsA( dem_object, CCA_MODE, (ULONG)dem_mode );
  403.  
  404. };
  405. ///
  406.  
  407. /// dem_EnterPassword
  408. void dem_EnterPassword( APTR dem_object ) {
  409. TEXT dem_buffer[50];
  410.  
  411.   printf( "\nEnter a new password => " );
  412.   fflush( stdout );
  413.   help_ReadString( dem_buffer );
  414.  
  415.   // the passed string will be copied to the internal buffer.
  416.   help_SetAttrsA( dem_object, CCA_PASSWORD, (ULONG)dem_buffer );
  417.  
  418. };
  419. ///
  420.  
  421. /// dem_ToggleHook
  422. void dem_ToggleHook( APTR dem_object ) {
  423. ULONG dem_hook;
  424.  
  425.   // switch between progresshook on and progresshook off
  426.   GetAttr( CCA_PROGRESSHOOK, dem_object, &dem_hook );
  427.   help_SetAttrsA( dem_object, CCA_PROGRESSHOOK, dem_hook ? NULL : (ULONG)&glo_clihook );
  428.  
  429.   if (dem_hook != NULL)
  430.     printf( "\nCLI-Progress function has been removed !\n" );
  431.   else
  432.     printf( "\nCLI-Progress function has been installed !\n" );
  433.  
  434.   help_WaitReturn();
  435.  
  436. };
  437. ///
  438.  
  439. /// dem_PopupGUI
  440. void dem_PopupGUI( APTR dem_object ) {
  441. struct Msg pop_msg;
  442.  
  443.   pop_msg.MethodID = CCM_PREFSGUI;
  444.  
  445.   // the simpliest way to do the configuration
  446.   if (DoMethodA( dem_object, (Msg)&pop_msg ) != NULL) {
  447.  
  448.     // Damn, something went wrong
  449.     printf( "\nCannot launch GUI !\n" );
  450.     help_WaitReturn();
  451.  
  452.   };
  453.  
  454. };
  455. ///
  456.  
  457. /// dem_HiddenPassword
  458. void dem_HiddenPassword( APTR dem_object ) {
  459. ULONG dem_hidden;
  460.  
  461.   GetAttr( CCA_HIDEPASSWORD, dem_object, &dem_hidden );
  462.   help_SetAttrsA( dem_object, CCA_HIDEPASSWORD, !dem_hidden );
  463.  
  464.   if (dem_hidden != NULL)
  465.     printf( "\nThe password in the GUI is now visible !\n" );
  466.   else
  467.     printf( "\nThe password in the GUI is now invisible !\n" );
  468.  
  469.   help_WaitReturn();
  470.  
  471. };
  472. ///
  473.  
  474. /// dem_ToggleProgress
  475. void dem_ToggleProgress( APTR dem_object ) {
  476. ULONG dem_flags;
  477.  
  478.   GetAttr( CCA_INTERNALPROGRESS, dem_object, &dem_flags );
  479.   if (dem_flags != FALSE) {
  480.     help_SetAttrsA( dem_object, CCA_INTERNALPROGRESS, FALSE );
  481.     printf( "\nInternal Progress-Report removed !\n" );
  482.   } else {
  483.     help_SetAttrsA( dem_object, CCA_INTERNALPROGRESS, TRUE );
  484.     printf( "\nInternal Progress-Report installed !\n" );
  485.   };
  486.  
  487.   help_WaitReturn();
  488.  
  489. };
  490. ///
  491.  
  492. /// dem_LoadPrefs
  493. // Both procedures are using the "iffparse.library" to write
  494. // the IFF-File. You could use your own code instead to write
  495. // such an IFF-File but I hope you won't write the chunk as
  496. // raw data in your prefsfile. However, this is a little example
  497. // that has minimal functionality but it shows how it works.
  498. void dem_LoadPrefs( APTR dem_object ) {
  499. TEXT                      dem_path[200];
  500. struct StoredProperty     *dem_prhd;
  501. struct StoredProperty     *dem_cccp;
  502. struct IFFHandle          *dem_iff;
  503. struct PrefHeader         *dem_prefhd;
  504. ULONG                     dem_res;
  505.  
  506.   printf( "\nEnter path of the prefsfile => " );
  507.   fflush( stdout );
  508.   help_ReadString( dem_path );
  509.  
  510.   dem_iff = AllocIFF();
  511.   if (dem_iff != NULL) {
  512.  
  513.     dem_iff->iff_Stream = Open( dem_path, MODE_OLDFILE );
  514.     if (dem_iff->iff_Stream != NULL) {
  515.  
  516.       InitIFFasDOS( dem_iff );
  517.       if (OpenIFF( dem_iff, IFFF_READ ) == 0) {
  518.  
  519.         StopOnExit( dem_iff, ID_PREF, ID_FORM );
  520.         PropChunk( dem_iff, ID_PREF, ID_PRHD );
  521.         PropChunk( dem_iff, ID_PREF, ID_CCCP );
  522.  
  523.         // search the selected chunks
  524.         dem_res  = ParseIFF( dem_iff, IFFPARSE_SCAN );
  525.         dem_prhd = FindProp( dem_iff, ID_PREF, ID_PRHD );
  526.         dem_cccp = FindProp( dem_iff, ID_PREF, ID_CCCP );
  527.  
  528.         // valid chunks ?
  529.         if (((dem_res == IFFERR_EOF) || (dem_res == IFFERR_EOC)) && (dem_prhd != NULL) && (dem_cccp != NULL)) {
  530.  
  531.           dem_prefhd = (PrefHeader *)dem_prhd->sp_Data;
  532.           printf( "\nVersion of the prefsfile : %ld\n", dem_prefhd->ph_Version );
  533.  
  534.           // here ! this is a simple way of setting the prefs.
  535.           help_SetAttrsA( dem_object, CCA_PREFSCHUNK, (ULONG)dem_cccp );
  536.           printf( "Loaded prefs are setted !\n" );
  537.           help_WaitReturn();
  538.  
  539.         };
  540.  
  541.         CloseIFF( dem_iff );
  542.  
  543.       };
  544.       Close( dem_iff->iff_Stream );
  545.  
  546.     };
  547.     else {
  548.       printf( "\nCannot open file '\s' !\n", dem_path );
  549.       help_WaitReturn();
  550.     };
  551.  
  552.     FreeIFF( dem_iff );
  553.  
  554.   };
  555.   else {
  556.     printf( "\nCan't get an IFF-Handle !\n" );
  557.     help_WaitReturn();
  558.   };
  559.  
  560. };
  561. ///
  562.  
  563. /// dem_SavePrefs
  564. void dem_SavePrefs( APTR dem_object ) {
  565. TEXT                     dem_path[200];
  566. struct StoredProperty    *dem_cccp;
  567. struct IFFHandle         *dem_iff;
  568. struct PrefHeader        dem_prefhd;
  569.  
  570.   printf( "\nEnter path of the prefsfile => " );
  571.   fflush( stdout );
  572.   help_ReadString( dem_path );
  573.  
  574.   dem_iff = AllocIFF();
  575.   if (dem_iff != NULL) {
  576.  
  577.     dem_iff->iff_Stream = Open( dem_path, MODE_NEWFILE );
  578.     if (dem_iff->iff_Stream != NULL) {
  579.  
  580.       InitIFFasDOS( dem_iff );
  581.       if (OpenIFF( dem_iff, IFFF_WRITE ) == 0 ) {
  582.  
  583.         PushChunk( dem_iff, ID_PREF, ID_FORM, IFFSIZE_UNKNOWN );
  584.  
  585.         dem_prefhd.ph_Version = 1;
  586.         dem_prefhd.ph_Type    = 0;
  587.         dem_prefhd.ph_Flags   = 0;
  588.  
  589.         PushChunk( dem_iff, ID_PREF, ID_PRHD, 6 );
  590.         WriteChunkBytes( dem_iff, &dem_prefhd, 6 );
  591.         PopChunk( dem_iff );
  592.  
  593.         // this chunk must be saved
  594.         GetAttr( CCA_PREFSCHUNK, dem_object, (ULONG *)&dem_cccp );
  595.  
  596.         // here we are storing our chunk
  597.         PushChunk( dem_iff, ID_PREF, ID_CCCP, dem_cccp->sp_Size );
  598.         WriteChunkBytes( dem_iff, dem_cccp->sp_Data, dem_cccp->sp_Size );
  599.         PopChunk( dem_iff );
  600.  
  601.         PopChunk( dem_iff );
  602.  
  603.         CloseIFF( dem_iff );
  604.  
  605.         printf( "\nWriting the prefs was successful !\n" );
  606.         help_WaitReturn();
  607.  
  608.       };
  609.       Close( dem_iff->iff_Stream );
  610.  
  611.     };
  612.     else {
  613.       printf( "\nCannot open file '\s' !\n", dem_path );
  614.       help_WaitReturn();
  615.     };
  616.  
  617.     FreeIFF( dem_iff );
  618.  
  619.   };
  620.   else {
  621.     printf( "\nCan't get an IFF-Handle !\n" );
  622.     help_WaitReturn();
  623.   };
  624.  
  625. };
  626. ///
  627.  
  628. /// dem_ToggleMemPool
  629. void dem_ToggleMemPool( APTR dem_object ) {
  630. ULONG dem_pool;
  631.  
  632.   GetAttr( CCA_MEMPOOL, dem_object, &dem_pool );
  633.   help_SetAttrsA( dem_object, CCA_MEMPOOL, dem_pool ? NULL : (ULONG)glo_mempool );
  634.  
  635.   if (dem_pool != NULL)
  636.     printf( "\nMemory pool has been removed !\n" );
  637.   else
  638.     printf( "\nMemory pool has been installed !\n" );
  639.  
  640.   help_WaitReturn();
  641.  
  642. };
  643. ///
  644.  
  645. /// dem_File2File
  646. void dem_File2File( APTR dem_object, ULONG dem_compressing ) {
  647. struct ccmFile2File     dem_msg;
  648. TEXT                    dem_infile[200];
  649. TEXT                    dem_outfile[200];
  650. LONG                    dem_xerr;
  651.  
  652.   printf( "\nPath of the file to " );
  653.   if (dem_compressing == FALSE) printf( "de" );
  654.   printf( "compress => " );
  655.   fflush( stdout );
  656.   help_ReadString( dem_infile );
  657.  
  658.   printf( "Path of the destination-file => " );
  659.   fflush( stdout );
  660.   help_ReadString( dem_outfile );
  661.  
  662.   printf( "\n" );
  663.  
  664.   // here a file will be compressed
  665.   dem_msg.methodid        = CCM_FILE2FILE;
  666.   dem_msg.com_Source      = dem_infile;
  667.   dem_msg.com_Destination = dem_outfile;
  668.   dem_msg.com_Compressing = dem_compressing;
  669.  
  670.   dem_xerr = DoMethodA( dem_object, (Msg)&dem_msg );
  671.   if (dem_xerr == FALSE) {
  672.     printf( "\nFile '%s' was ", dem_infile );
  673.     if (dem_compressing == FALSE) printf( "de" );
  674.     printf( "compressed from %lu to %lu Bytes\n", help_FileLength( dem_infile ), help_FileLength( dem_outfile ) );
  675.   };
  676.   else {
  677.     printf( "\nDamn, an error occured ! XPK-Error = " );
  678.     help_ShowXPKError( dem_xerr );
  679.   };
  680.  
  681.   help_WaitReturn();
  682.  
  683. };
  684. ///
  685.  
  686. /// dem_File2Mem
  687. void dem_File2Mem( APTR dem_object, ULONG dem_compressing ) {
  688. TEXT                  dem_infile[200];
  689. struct ccmFile2Mem    dem_file2mem;
  690. APTR                  dem_pool;
  691. ULONG                 dem_mem;
  692. ULONG                 dem_length;
  693. ULONG                 dem_endlen;
  694. LONG                  dem_xerr;
  695.  
  696.   printf( "\nPath of the file to " );
  697.   if (dem_compressing == FALSE) printf( "de" );
  698.   printf( "compress => " );
  699.   fflush( stdout );
  700.   help_ReadString( dem_infile );
  701.  
  702.   if (help_FileLength( dem_infile ) <= 0) {
  703.     printf( "\nFile '%s' doesn't exist !\n", dem_infile );
  704.     help_WaitReturn();
  705.     return;
  706.   };
  707.  
  708.   GetAttr( CCA_MEMPOOL, dem_object, (ULONG *)&dem_pool );
  709.   if (dem_pool != NULL) {
  710.  
  711.     // a memory pool is installed, so we are
  712.     // using this pool
  713.     dem_file2mem.com_Memory = &dem_mem;
  714.     dem_file2mem.com_Length = 0;          // this is needed to use the pool
  715.  
  716.   };
  717.   else {
  718.  
  719.     // we must allocate the memory by ourself, so first
  720.     // we need to find out the length
  721.     if (dem_compressing != FALSE)
  722.       dem_length = PACKSIZE( help_FileLength( dem_infile ) );
  723.     else {
  724.  
  725.       struct ccmExamine   dem_examine;
  726.  
  727.       dem_examine.methodid      = CCM_EXAMINE;
  728.       dem_examine.com_Source    = dem_infile;
  729.       dem_examine.com_Memory    = NULL;
  730.       dem_examine.com_MemoryLen = 0;
  731.       dem_examine.com_SizeAddr  = &dem_length;
  732.  
  733.       DoMethodA( dem_object, (Msg)&dem_examine );
  734.       dem_length = UNPACKSIZE( dem_length );
  735.  
  736.     };
  737.  
  738.     // now do the allocation
  739.     dem_mem = (ULONG)AllocMem( dem_length, MEMF_PUBLIC );
  740.     if (dem_mem == NULL) {
  741.       printf( "\nCannot allocate enough memory !\n" );
  742.       help_WaitReturn();
  743.       return;
  744.     };
  745.  
  746.     dem_file2mem.com_Memory = (APTR)dem_mem;
  747.     dem_file2mem.com_Length = dem_length;
  748.  
  749.   };
  750.  
  751.   dem_file2mem.methodid        = CCM_FILE2MEM;
  752.   dem_file2mem.com_Compressing = dem_compressing;
  753.   dem_file2mem.com_Source      = dem_infile;
  754.   dem_file2mem.com_OutLen      = &dem_endlen;
  755.  
  756.   // call the method
  757.   dem_xerr = DoMethodA( dem_object, (Msg)&dem_file2mem );
  758.   if (dem_xerr == FALSE) {
  759.  
  760.     printf( "\nCompression was successful !\n" );
  761.     if (dem_pool != NULL) {
  762.       dem_length = MEMSIZE( dem_mem );
  763.       printf( "Memory pool was used !\n" );
  764.     };
  765.     printf( "Memory area at $%08lx ( %lu Bytes )\n", dem_mem, dem_length );
  766.     printf( "Compressed data %lu Bytes\n", dem_endlen );
  767.  
  768.   }; else {
  769.     printf( "\nAn error occured ! XPK-Error: " );
  770.     help_ShowXPKError( dem_xerr );
  771.   };
  772.  
  773.   if (dem_pool != NULL) {
  774.     if (dem_xerr == FALSE) {
  775.       dem_mem = dem_mem - 4;
  776.       FreePooled( glo_mempool, (APTR)dem_mem, dem_length );
  777.     };
  778.   }; else
  779.     FreeMem( (APTR)dem_mem, dem_length );
  780.  
  781.   help_WaitReturn();
  782.  
  783. };
  784. ///
  785.  
  786. /// dem_Mem2Mem
  787. void dem_Mem2Mem( APTR dem_object, ULONG dem_compressing ) {
  788. TEXT                  dem_infile[200];
  789. struct ccmMem2Mem     dem_mem2mem;
  790. APTR                  dem_pool;
  791. ULONG                 dem_mem;
  792. ULONG                 dem_length;
  793. ULONG                 dem_endlen;
  794. LONG                  dem_xerr;
  795.  
  796.   printf( "\nPath of the file to " );
  797.   if (dem_compressing == FALSE) printf( "de" );
  798.   printf( "compress => " );
  799.   fflush( stdout );
  800.   help_ReadString( dem_infile );
  801.  
  802.   help_LoadFile( dem_infile );
  803.   if (glo_filemem == NULL) {
  804.     printf( "\nFile '%s' doesn't exist !\n", dem_infile );
  805.     help_WaitReturn();
  806.     return;
  807.   };
  808.  
  809.   GetAttr( CCA_MEMPOOL, dem_object, (ULONG *)&dem_pool );
  810.   if (dem_pool != NULL) {
  811.  
  812.     // a memory pool is installed, so we are
  813.     // using this pool
  814.     dem_mem2mem.com_Destination    = &dem_mem;
  815.     dem_mem2mem.com_DestinationLen = 0;          // this is needed to use the pool
  816.  
  817.   };
  818.   else {
  819.  
  820.     // we must allocate the memory by ourself, so first
  821.     // we need to find out the length
  822.     if (dem_compressing != FALSE)
  823.       dem_length = PACKSIZE( glo_filesize );
  824.     else {
  825.  
  826.       struct ccmExamine   dem_examine;
  827.  
  828.       dem_examine.methodid      = CCM_EXAMINE;
  829.       dem_examine.com_Source    = NULL;
  830.       dem_examine.com_Memory    = glo_filemem;
  831.       dem_examine.com_MemoryLen = glo_filesize;
  832.       dem_examine.com_SizeAddr  = &dem_length;
  833.  
  834.       DoMethodA( dem_object, (Msg)&dem_examine );
  835.       dem_length = UNPACKSIZE( dem_length );
  836.  
  837.     };
  838.  
  839.     // now do the allocation
  840.     dem_mem = (ULONG)AllocMem( dem_length, MEMF_PUBLIC );
  841.     if (dem_mem == NULL) {
  842.       printf( "\nCannot allocate enough memory !\n" );
  843.       help_WaitReturn();
  844.       return;
  845.     };
  846.  
  847.     dem_mem2mem.com_Destination    = (APTR)dem_mem;
  848.     dem_mem2mem.com_DestinationLen = dem_length;
  849.  
  850.   };
  851.  
  852.   dem_mem2mem.methodid        = CCM_MEM2MEM;
  853.   dem_mem2mem.com_Compressing = dem_compressing;
  854.   dem_mem2mem.com_Source      = glo_filemem;
  855.   dem_mem2mem.com_SourceLen   = glo_filesize;
  856.   dem_mem2mem.com_OutLen      = &dem_endlen;
  857.  
  858.   // call the method
  859.   dem_xerr = DoMethodA( dem_object, (Msg)&dem_mem2mem );
  860.   if (dem_xerr == FALSE) {
  861.  
  862.     printf( "\nCompression was successful !\n" );
  863.     if (dem_pool != NULL) {
  864.       dem_length = MEMSIZE( dem_mem );
  865.       printf( "Memory pool was used !\n" );
  866.     };
  867.     printf( "Memory area $%08lx ( %lu Bytes ) to $%08lx ( %lu Bytes )\n", glo_filemem, glo_filesize, dem_mem, dem_length );
  868.  
  869.   }; else {
  870.     printf( "\nAn error occured ! XPK-Error: " );
  871.     help_ShowXPKError( dem_xerr );
  872.   };
  873.  
  874.   if (dem_pool != NULL) {
  875.     if (dem_xerr == FALSE) {
  876.       dem_mem = dem_mem - 4;
  877.       FreePooled( glo_mempool, (APTR)dem_mem, dem_length );
  878.     };
  879.   }; else
  880.     FreeMem( (APTR)dem_mem, dem_length );
  881.  
  882.   help_WaitReturn();
  883.  
  884. };
  885. ///
  886.  
  887. /// dem_Menu
  888. void dem_Menu( APTR dem_object ) {
  889. ULONG dem_selected;
  890. UBYTE dem_end;
  891.  
  892.   dem_end = 0;
  893.  
  894.   for(;;) {
  895.  
  896.     help_ClearCON
  897.     printf( "          +-------------------------------------+\n" );
  898.     printf( "          |             %c[1;1mCONFIGURATION%c[0m           |\n", 27, 27 );
  899.     printf( "          +-------------------------------------+\n" );
  900.     printf( "          | 01. Show configuration              |\n" );
  901.     printf( "          | 02. Select a method                 |\n" );
  902.     printf( "          | 03. Select the mode                 |\n" );
  903.     printf( "          | 04. Enter a password                |\n" );
  904.     printf( "          | 05. Toggle hook installation        |\n" );
  905.     printf( "          | 06. Use GUI for configuration       |\n" );
  906.     printf( "          | 07. Toggle hidden password          |\n" );
  907.  
  908.     if (iffparsebase != NULL) {
  909.       printf( "          | 08. Load configuration from file    |\n" );
  910.       printf( "          | 09. Save configuration to file      |\n" );
  911.     };
  912.  
  913.     printf( "          | 10. Toggle memory pool installation |\n" );
  914.     printf( "          | 19. Toggle internal progress report |\n" );
  915.     printf( "          +-------------------------------------+\n\n" );
  916.     printf( "+---------------------------+ +---------------------------+\n" );
  917.     printf( "|         %c[1;1mCOMPRESSION%c[0m       | |        %c[1;1mDECOMPRESSION%c[0m      |\n", 27, 27, 27, 27 );
  918.     printf( "+---------------------------+ +---------------------------+\n" );
  919.     printf( "| 11. From file to file     | | 14. From file to file     |\n" );
  920.     printf( "| 12. From file to memory   | | 15. From file to memory   |\n" );
  921.     printf( "| 13. From memory to memory | | 16. From memory to memory |\n" );
  922.     printf( "+-------------+-------------+-+----------+----------------+\n" );
  923.     printf( "              |  %c[2;1m20. Leave this program%c[0m  |\n", 27, 27 );
  924.     printf( "              +--------------------------+\n\n" );
  925.     printf( "  Your choice => " );
  926.     fflush( stdout );
  927.  
  928.     dem_selected = help_ReadValue();
  929.  
  930.     help_ClearCON
  931.     switch( dem_selected ) {
  932.     case  1  : dem_ShowConfiguration ( dem_object ); break;
  933.     case  2  : dem_SelectMethod      ( dem_object ); break;
  934.     case  3  : dem_SelectMode        ( dem_object ); break;
  935.     case  4  : dem_EnterPassword     ( dem_object ); break;
  936.     case  5  : dem_ToggleHook        ( dem_object ); break;
  937.     case  6  : dem_PopupGUI          ( dem_object ); break;
  938.     case  7  : dem_HiddenPassword    ( dem_object ); break;
  939.     case 10  : dem_ToggleMemPool     ( dem_object ); break;
  940.     case  8  : if (iffparsebase != NULL) dem_LoadPrefs( dem_object ); break;
  941.     case  9  : if (iffparsebase != NULL) dem_SavePrefs( dem_object ); break;
  942.     case 11  : dem_File2File ( dem_object, TRUE  ); break;
  943.     case 14  : dem_File2File ( dem_object, FALSE ); break;
  944.     case 12  : dem_File2Mem  ( dem_object, TRUE  ); break;
  945.     case 15  : dem_File2Mem  ( dem_object, FALSE ); break;
  946.     case 13  : dem_Mem2Mem   ( dem_object, TRUE  ); break;
  947.     case 16  : dem_Mem2Mem   ( dem_object, FALSE ); break;
  948.     case 19  : dem_ToggleProgress( dem_object ); break;
  949.     case 20  : dem_end = 1 ; break ;
  950.     default  : break ;
  951.     };
  952.  
  953.     if (dem_end != 0) break;
  954.  
  955.   };
  956.  
  957. };
  958. ///
  959.  
  960.  
  961. /* -- ----------------------------------------------------------------- -- *
  962.  * --                               Main                                -- *
  963.  * -- ----------------------------------------------------------------- -- */
  964.  
  965. /// main
  966. ULONG main( void ) {
  967. struct IClass     *ma_class;
  968. struct Process    *ma_myproc;
  969. struct TagItem    ma_tags[5];
  970. ULONG             ma_retcode;
  971. APTR              ma_object;
  972.  
  973.   // this is useful for later, except C initialises
  974.   // global variables by default to NULL but I don't know if so 8-(
  975.   ma_retcode   = 0;
  976.  
  977.   // check whether we are being started from CLI or not
  978.   ma_myproc = (struct Process *)FindTask( NULL );
  979.   if (ma_myproc->pr_CLI == NULL)
  980.     {
  981.       printf( "Start me from the CLI !\n" );
  982.       return 20UL;
  983.     };
  984.  
  985.   // try to open the "iffparse.library". It does no matter
  986.   // if this opening fails.
  987.   iffparsebase = OpenLibrary( "iffparse.library", 37 );
  988.  
  989.   gfxbase = OpenLibrary( "graphics.library", 37 );
  990.   if (gfxbase != NULL) {
  991.  
  992.     dosbase = OpenLibrary( "dos.library", 37 );
  993.     if (dosbase != NULL) {
  994.  
  995.       intuitionbase = OpenLibrary( "intuition.library", 33 );
  996.       if (intuitionbase != NULL) {
  997.  
  998.         compressorbase = OpenLibrary( "compressor.class", 1 );
  999.         if (compressorbase != NULL) {
  1000.  
  1001.           ma_class = Cc_GetClassPtr();
  1002.           if (ma_class != NULL) {
  1003.  
  1004.             // create a memory pool
  1005.             glo_mempool = CreatePool( MEMF_PUBLIC, 500000, 25000 );
  1006.  
  1007.             ma_tags[0].ti_Tag  = CCA_METHOD;
  1008.             ma_tags[1].ti_Tag  = CCA_MODE;
  1009.             ma_tags[2].ti_Tag  = CCA_PASSWORD;
  1010.             ma_tags[3].ti_Tag  = CCA_PROGRESSHOOK;
  1011.             ma_tags[4].ti_Tag  = TAG_END;
  1012.             ma_tags[0].ti_Data = (ULONG)"HUFF";
  1013.             ma_tags[1].ti_Data = 80;
  1014.             ma_tags[2].ti_Data = (ULONG)"Peter Lustig";
  1015.             ma_tags[3].ti_Data = (ULONG)&glo_clihook;
  1016.             ma_tags[4].ti_Data = NULL;
  1017.  
  1018.             ma_object = NewObjectA( ma_class, NULL, &ma_tags[0] );
  1019.             if (ma_object != NULL) dem_Menu( ma_object );
  1020.  
  1021.             DisposeObject( ma_object );
  1022.  
  1023.             // free the last mem allocated for "help_LoadFile()"
  1024.             if (glo_filemem != NULL) FreeMem( glo_filemem, glo_filesize );
  1025.  
  1026.             // free all memory associated with this pool
  1027.             if (glo_mempool != NULL) DeletePool( glo_mempool );
  1028.  
  1029.           };
  1030.           else
  1031.             printf( "Class is not available !\n" );
  1032.  
  1033.           CloseLibrary( compressorbase );
  1034.         };
  1035.         else
  1036.           printf( "Cannot open the 'compressor.class' v1.0 or higher !\n" );
  1037.  
  1038.         CloseLibrary( intuitionbase );
  1039.       };
  1040.       else
  1041.         ma_retcode = 20;
  1042.  
  1043.       CloseLibrary( dosbase );
  1044.     };
  1045.     else
  1046.       ma_retcode = 20;
  1047.  
  1048.     CloseLibrary( gfxbase );
  1049.   };
  1050.   else
  1051.     ma_retcode = 20;
  1052.  
  1053.   // close the "iffparse.library" if open
  1054.   if (iffparsebase != NULL) CloseLibrary( iffparsebase );
  1055.  
  1056.   exit( ma_retcode );
  1057.  
  1058. };
  1059. ///
  1060.  
  1061.  
  1062. /* -- ----------------------------------------------------------------- -- *
  1063.  * --                               Data                                -- *
  1064.  * -- ----------------------------------------------------------------- -- */
  1065.  
  1066. TEXT glo_versionstr[] = "$VER: com_TestClass.e 1.0 (17-Sep-98) [ Daniel Kasmeroglu ]";
  1067.  
  1068.